(function (vc) {

    vc.extends({
        data: {
            addServRecommendInfo: {
                communityId: '',
                communityName: '',
                shopName: '',
                shopId: '',
                servId: '',
                recommendType: 'SERV',
                logo: '',
                prodName: '',
                price: '',
                url: '',
                shops: [],
                servs: [],
            }
        },
        _initMethod: function () {
           
        },
        _initEvent: function () {
            vc.on("addServRecommend", "notifyUploadCoverImage", function (_param) {
                if (_param) {
                    $that.addServRecommendInfo.logo = _param[0].url
                } else {
                    $that.addServRecommendInfo.logo = '';
                }
            });
            vc.on('addServRecommend', 'openAddServRecommendModal', function (_param) {
                $that.addServRecommendInfo.communityId = _param.communityId;
                $that.addServRecommendInfo.communityName = _param.communityName;
                $that._listAServRecommendShops();
                vc.emit('addServRecommend', 'uploadImageUrl', 'notifyPhotos', []);
                $('#addServRecommendModel').modal('show');
            });
        },
        methods: {
            addServRecommendValidate() {
                return vc.validate.validate({
                    addServRecommendInfo: $that.addServRecommendInfo
                }, {
                    'addServRecommendInfo.logo': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "推荐logo不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "推荐logo不能超过512"
                        },
                    ],
                    'addServRecommendInfo.prodName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商品名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "256",
                            errInfo: "商品名称不能超过256"
                        },
                    ],
                    'addServRecommendInfo.price': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "价格不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "10",
                            errInfo: "价格不能超过10"
                        },
                    ],
                });
            },
            saveServRecommend: function () {
                if (!$that.addServRecommendValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.addServRecommendInfo.url = '/pages/homemaking/detail?servId=' + $that.addServRecommendInfo.servId + '&shopId=' + $that.addServRecommendInfo.shopId;;
                vc.http.apiPost(
                    '/recommend.saveCommunityRecommend',
                    JSON.stringify($that.addServRecommendInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addServRecommendModel').modal('hide');
                            $that.clearAddServRecommendInfo();
                            vc.emit('communityRecommend', 'listCommunityRecommend', {});
                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);

                    });
            },
            clearAddServRecommendInfo: function () {
                $that.addServRecommendInfo = {
                    communityId: '',
                    communityName: '',
                    shopName: '',
                    shopId: '',
                    servId: '',
                    recommendType: 'SERV',
                    logo: '',
                    prodName: '',
                    price: '',
                    url: '',
                    shops: [],
                    servs: [],
                };
            },
            _listAServRecommendShops: function () {
                let param = {
                    params: {
                        page: 1,
                        row: 100
                    }
                };
                //发送get请求
                vc.http.apiGet('/shop.queryShopsByAdmin',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addServRecommendInfo.shops = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            queryAShopServs: function () {
                let _shops = $that.addServRecommendInfo.shops;
                let _shopId = $that.addServRecommendInfo.shopId;
                _shops.forEach(_s => {
                    if (_s.shopId == _shopId) {
                        $that.addServRecommendInfo.shopName = _s.shopName;
                    }
                });
                $that._listAShopServ();
            },
            _listAShopServ: function () {
                let param = {
                    params: {
                        page: 1,
                        row: 500,
                        shopId: $that.addServRecommendInfo.shopId,
                    }
                };
                //发送get请求
                vc.http.apiGet('/housekeepingServ.queryAdminServ',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addServRecommendInfo.servs = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            selectServs: function () {
                let _servs = $that.addServRecommendInfo.servs;
                let _servId = $that.addServRecommendInfo.servId;
                _servs.forEach(_p => {
                    if (_p.servId == _servId) {
                        $that.addServRecommendInfo.prodName = _p.servName;
                        $that.addServRecommendInfo.price = _p.defaultFee;
                        $that.addServRecommendInfo.logo = _p.coverPhoto;
                        let _photos = [];
                        _photos.push(_p.coverPhoto);
                        vc.emit('addServRecommend', 'uploadImageUrl', 'notifyPhotos', _photos);
                    }
                });
            }
        }
    });

})(window.vc);
